import DlyadminAigoods, { typeMap } from '../../model/dlyadminAigoods'

import ApiError from '../../error/ApiError'
import ApiErrorNames from '../../error/ApiErrorNames'


class AiGoodsControlle {

	static async list(ctx) {
		// 获取列表全部数据
		const { status, type } = ctx.request.query || {}
		console.log('123456', type)
		const getTapol = {
			status: status ? status : '',
			type: type ? (type === 'all' ? '' : type) : 'ai',
		}
		for (let key in getTapol) {
			if (getTapol[key] == '') {
				delete getTapol[key]
			}
		}
		if (getTapol.type && !typeMap.has(getTapol.type)) {
			return ctx.body = { list: null, total: 0 }
		}
		const result = await DlyadminAigoods.find(getTapol).sort({ sort: -1 }).limit(1000).skip(0)
		const total = await DlyadminAigoods.find(getTapol).countDocuments()
		return ctx.body = { list: result, total }
	}

	static async add(ctx) {
		console.log('ctx.request.body', ctx.request.body)
		const {
			name = '',
			type = '',
			price = '',
			integral = '',
			img = '',
			status = '',
			stock = '',
			sales = '',
			detail = '',
			giveintegral = '',
			timenum = '',
			selectTiemtype = '',
			lineprice = '',
			sort = '0',
		} = ctx.request.body || {}
		if (!name || !type || !price || !integral) {
			// 缺少必要参数
			throw new ApiError(ApiErrorNames.NoParamsData)
		}
		const addData = {
			name,
			type,
			price,
			integral,
			img,
			status,
			stock,
			sales,
			detail,
			giveintegral,
			timenum,
			selectTiemtype,
			lineprice,
			sort
		}
		for (let key in addData) {
			if (addData[key] == '') {
				delete addData[key]
			}
		}
		const result = await DlyadminAigoods.create(addData)
		if (result) {
			return ctx.body = '添加成功'
		}
		throw new ApiError(ApiErrorNames.ADD_GOODS_FAIL)
	}
	static async edit(ctx) {
		// 编辑列表
		if (!ctx.params.id) {
			throw new ApiError(ApiErrorNames.NoParamsData)
		}
		const {
			name = '',
			type = '',
			price = '',
			integral = '',
			img = '',
			status = '',
			stock = '',
			sales = '',
			detail = '',
			giveintegral = '',
			sort = '0',
			selectTiemtype = '',
			timenum = '',
			lineprice = '',
		} = ctx.request.body || {}
		if (!name) {
			// 缺少必要参数
			throw new ApiError(ApiErrorNames.NoParamsData)
		}
		const editData = {
			name,
			type,
			price,
			integral,
			img,
			status,
			stock,
			sales,
			detail,
			giveintegral,
			sort,
			selectTiemtype,
			timenum,
			lineprice
		}
		for (let key in editData) {
			if (editData[key] == '') {
				delete editData[key]
			}
		}
		const id = ctx.params.id
		const result = await DlyadminAigoods.updateOne({ _id: id }, editData)
		if (result.nModified === 0) {

			throw new ApiError(ApiErrorNames.NoData)
		}
		return ctx.body = '修改成功'
	}
	static async deleted(ctx) {
		if (!ctx.params.id) {
			throw new ApiError(ApiErrorNames.NoParamsData)
		}
		try {
			const id = ctx.params.id
			const result = await DlyadminAigoods.deleteOne({ _id: id })
			if (result.deletedCount === 0) {
				throw new ApiError(ApiErrorNames.NoData)
			}
			// upDataWhiteList()
			ctx.body = 'ok'
		} catch (error) {
			throw new ApiError(ApiErrorNames.UnknownError)
		}
	}

}

export default AiGoodsControlle
